package com.itqf.dao.impl;

import com.itqf.connecter.DbConnection;
import com.itqf.dao.AddressDao;
import com.itqf.dao.GoodsDao;
import com.itqf.dao.OrderDao;
import com.itqf.entity.Address;
import com.itqf.entity.Goods;
import com.itqf.entity.Item;
import com.itqf.entity.Order;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public void insertOrder(Order order) {
        Connection conn=null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int result = 0;
        conn= DbConnection.getConnection();
        String sql ="insert into orders (o_id,address_id,userno,g_id , ocount ,o_status,o_time) values (?,?,?,?,?,?,?)";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,order.getOid());
            ps.setInt(2,order.getAddressid());
            ps.setInt(3,order.getUserno());
            ps.setInt(4,order.getGid());
            ps.setBigDecimal(5,order.getCount());
            ps.setInt(6,order.getOstatus());
            ps.setDate(7, new java.sql.Date(order.getOtime().getTime()));
            result = ps.executeUpdate();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        DbConnection.close(rs, ps);
        System.out.println("操作结果："+result);

    }

    @Override
    public void insertItems(List<Item> items) {
        String sql="insert into item (o_id,g_id,icount,number) value (?,?,?,?)";

        for (int i=0;i<items.size();i++) {
            Item item = items.get(i);
            Connection conn=null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            int result = 0;
            conn= DbConnection.getConnection();

            try {
                ps = conn.prepareStatement(sql);
                ps.setString(1,item.getOid());
                ps.setInt(2,item.getGid());
                ps.setBigDecimal(3,item.getCount());
                ps.setInt(4,item.getNumber());

                result = ps.executeUpdate();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            DbConnection.close(rs, ps);
        }

    }

    @Override
    public List<Order> selectOrderByUserno(int userno) {
        String sql = "SELECT o_id as oid , userno as userno , g_id as gid , ocount , address_id as addressid , o_time as otime , o_status as ostatus from orders WHERE userno = ? ";
        Connection conn=null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn= DbConnection.getConnection();
        List<Order> list = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, userno);
            rs = ps.executeQuery();
            while(rs.next()) {
                String oid = rs.getString(1);
                int gid = rs.getInt(3);
                BigDecimal count = rs.getBigDecimal(4);
                int addressid = rs.getInt(5);
                AddressDao addressDao = new AddressDaoImpl();
                Address address = addressDao.selectAddressesByAddressId(addressid);
                Date otime = rs.getDate(6);
                int ostatus = rs.getInt(7);
                Order order = new Order(oid, userno,gid,count, addressid, address, otime,ostatus);
                list.add(order);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }finally{
            DbConnection.close(rs, ps);}
        return list;
    }

    @Override
    public Order selectOrderByOid(String oid) {
        String sql = "SELECT * FROM orders WHERE o_id = ? ";
        Connection conn=null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn= DbConnection.getConnection();
        Order order = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, oid);
            rs = ps.executeQuery();
            while(rs.next()) {
                int userno = rs.getInt(2);
                int gid = rs.getInt(3);
                BigDecimal count = rs.getBigDecimal(4);
                int addressid = rs.getInt(5);
                AddressDao addressDao = new AddressDaoImpl();
                Address address = addressDao.selectAddressesByAddressId(addressid);
                Date otime = rs.getDate(6);
                int ostatus = rs.getInt(7);
                order = new Order(oid, userno,gid,count, addressid, address, otime,ostatus);
                System.out.println("测试1"+order.toString());
            }
        }catch(Exception e) {
            e.printStackTrace();
        }finally{
            DbConnection.close(rs, ps);}
        return order;
    }

    @Override
    public List<Item> selectItemsByOid(String oid) {
        String sql = "SELECT * FROM item WHERE o_id = ? ";
        Connection conn=null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn= DbConnection.getConnection();
        List<Item> list = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, oid);
            rs = ps.executeQuery();
            while(rs.next()) {
                int iid = rs.getInt(1);
                int gid = rs.getInt(3);
                GoodsDao goodsDao = new GoodsDaoImpl();
                Goods goods = goodsDao.selectGoodsByGid(gid);
                System.out.println(goods.toString());
                BigDecimal count = rs.getBigDecimal(4);
                int number  = rs.getInt(5);
                Item item = new Item(iid,oid,gid,goods,count,number);
                System.out.println("测试item"+item.toString());
                list.add(item);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }finally{
            DbConnection.close(rs, ps);}
        return list;
    }

}
